package com.pan.admin.generator.strategy;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.io.IOUtils;
import com.pan.admin.common.constant.Constants;
import com.pan.admin.common.utils.StringUtils;
import com.pan.admin.generator.config.ApiGenFieldTemplateConfig;
import com.pan.admin.generator.constant.ApiGenFieldEnum;
import com.pan.admin.generator.entity.ApiGenField;
import com.pan.admin.generator.entity.ApiGenTemplate;
import com.pan.admin.generator.util.ApiGenUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 *  API生成器-机构
 *
 *  @author Pan
 */
@Service
public class ApiGenOrgStrategy implements ApiGenStrategy {

    @Override
    public Map<String, String> viewTemplate(ApiGenTemplate apiGenTemplate, VelocityContext vmContext) {
        String customName = apiGenTemplate.getCustomName();

        //  获取模板对象
        Map<String, String> templateMap = this.listTemplate(customName);
        IteratorUtils.entrySet(templateMap, ((template, downloadPath) -> {
            //  渲染模板
            StringWriter sw = null;
            try {
                sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(vmContext, sw);
            } finally {
                IOUtils.close(sw);
            }
            templateMap.put(template, sw.toString());
        }));
        return templateMap;
    }

    @Override
    public void parseGenTemplateContext(ApiGenTemplate apiGenTemplate, VelocityContext vmContext) {
        vmContext.put("packageName", apiGenTemplate.getPackageName());
        vmContext.put("productId", apiGenTemplate.getProductId());
        vmContext.put("customChineseName", apiGenTemplate.getCustomChineseName());
        vmContext.put("customName", apiGenTemplate.getCustomName());

        //  撞库模板
        {
            List<ApiGenField> listGenDataExchangeField = apiGenTemplate.getListGenDataExchangeField();
            //  实体上下文
            String generateFieldEntityText = ApiGenUtils.generateFieldEntityText(listGenDataExchangeField, true);
            vmContext.put("genDataExchangeField", generateFieldEntityText);

            //  撞库自定义字段
            String generateCustomFieldEntityText = ApiGenUtils.generateFieldEntityText(apiGenTemplate.getListGenCustomDataExchangeField(), false);
            vmContext.put("genCustomDataExchange", generateCustomFieldEntityText);

            //  撞库属性赋值上下文
            String generateSetFieldText = ApiGenUtils.generateSetFieldText(listGenDataExchangeField, "orgDataExchange", "apiDataChangeMessage");
            vmContext.put("genDataExchangeSetField", generateSetFieldText);

            //  撞库表单赋值上下文
            String generateSetFieldFormText = ApiGenUtils.generateSetFormFieldText(listGenDataExchangeField, "orgDataExchange", "apiDataChangeMessage");
            vmContext.put("genDataExchangeSetFormField", generateSetFieldFormText);

            //  撞库响应验证赋值上下文
            String generateResponseFieldText = ApiGenUtils.generateResponseFieldText(apiGenTemplate.getDataExchangeResponse());
            vmContext.put("genDataExchangeResponseText", generateResponseFieldText);
        }

        //  进件模板
        {
            List<ApiGenField> listGenApplyField = apiGenTemplate.getListGenApplyField();
            //  实体上下文
            String genApplyFieldText = ApiGenUtils.generateFieldEntityText(listGenApplyField, true);
            vmContext.put("genApplyField", genApplyFieldText);

            //  进件自定义字段
            String genCustomApplyFieldText = ApiGenUtils.generateFieldEntityText(apiGenTemplate.getListGenCustomApplyField(), false);
            vmContext.put("genCustomApplyField", genCustomApplyFieldText);

            //  进件属性赋值上下文
            String genApplySetFieldText = ApiGenUtils.generateSetFieldText(listGenApplyField, "orgApply", "apiApplyParameter");
            vmContext.put("genApplySetField", genApplySetFieldText);

            //  进件表单赋值上下文
            String genApplySetFormFieldText = ApiGenUtils.generateSetFormFieldText(listGenApplyField, "orgApply", "apiApplyParameter");
            vmContext.put("genApplySetFormField", genApplySetFormFieldText);

            //  进件响应验证赋值上下文
            String genApplyResponseText = ApiGenUtils.generateResponseFieldText(apiGenTemplate.getApplyResponse());
            vmContext.put("genApplyResponse", genApplyResponseText);
        }
    }

    @Override
    public Map<String, String> listTemplate(String customName) {
        Map<String, String> templateMap = MapUtils.newLinkedHashMap();
        templateMap.put("vm/http/org/biz.java.vm",             "D:/gen/org/biz/ApiOrg" + customName + ".java");
        templateMap.put("vm/http/org/dataExchange.java.vm",    "D:/gen/org/entity/OrgDataExchange" + customName + ".java");
        templateMap.put("vm/http/org/apply.java.vm",           "D:/gen/org/entity/OrgApply" + customName + ".java");
        return templateMap;
    }

    @Override
    public String getFileName(String vmTemplateName, String customName) {
        String fileName = "http/org/{}/{}.java";
        //  撞库
        if (vmTemplateName.contains("dataExchange")) {
            return StringUtils.format(fileName, "entity", "OrgDataExchange" + customName);
        }
        //  进件
        if (vmTemplateName.contains("apply")) {
            return StringUtils.format(fileName, "entity", "OrgApply" + customName);
        }
        //  业务对接
        return StringUtils.format(fileName, "biz", "ApiOrg" + customName);
    }

    @Override
    public List<ApiGenField> listFieldTemplate(ApiGenFieldTemplateConfig apiGenFieldTemplateConfig) {
        List<ApiGenField> template = CollUtils.newArrayList();
        //  获取撞库字段模板
        if (apiGenFieldTemplateConfig.getHasDataExchange()) {
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PHONE_MD5));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PHONE_MASK));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PHONE));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CITY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CAR_INFO));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.REAL_ESTATE_INFO));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PROVIDENT_FUND));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.SOCIAL_SECURITY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.INSURANCE_POLICY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.AGE));
        }

        //  获取进件字段模板
        if (apiGenFieldTemplateConfig.getHasApply()) {
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PHONE));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CITY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.AGE));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.USER_NAME));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.IP));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.SEX));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.IDCARD));

            //  表单资质
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.SESAME_CREDIT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.REAL_ESTATE_INFO));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CAR_INFO));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.SOCIAL_SECURITY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.INSURANCE_POLICY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CAREER));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.MONTHLY_SALARY));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.SALARY_PAY_METHOD));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.BIZ_PUB_ACC_BANK_DETAILS));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.BIZ_BANK_DETAILS));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.BIZ_LICENSE_YEAR));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.LOAN_AMOUNT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.PROVIDENT_FUND));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.CREDIT_LIMIT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.HB_LIMIT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.BT_LIMIT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.OVERDUE));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.EDUCATION_ATTAINMENT));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.YEARS_SERVICE));
            template.add(ApiGenField.ofTemplate(ApiGenFieldEnum.LOAN_PURPOSE));
        }
        return template;
    }

}
